

import { tt } from "../../ttapi_interface/ttapi.js"
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
export class EUI_Style {
    static uiatals: tt.Atlas;
    static border: tt2.QUI_Scale9;
    static panelback1: tt2.QUI_Scale9;
    static panelback2: tt2.QUI_Scale9;
    static panelback3: tt2.QUI_Scale9;
    static paneltitle3: tt2.QUI_Scale9;
    static btnroundsmall_no2: tt.Sprite;
    static btnroundsmall2: tt.Sprite;
    static icon1: tt.Sprite;
    static icon2: tt.Sprite;
    static white: tt.Sprite;
    static tpool: tt.Texture8Pool;
    static font: tt.Font;
    static async Init(font: tt.Font): Promise<void> {
        this.font = font;
        this.tpool = new tt.Texture8Pool(256);

        this.white = new tt.Sprite(tt.graphic.getWhiteTexture(), this.tpool.PaletteTexture);

        this.uiatals = await tt2.QFrame_ResMgr.LoadAtlasAsync("./data/uires_pack0.png");

        this.border = new tt2.QUI_Scale9(this.uiatals.GetSprite("border"), new tt.Border(3, 3, 3, 3));
        this.panelback1 = new tt2.QUI_Scale9(this.uiatals.GetSprite("panelback1"), new tt.Border(3, 4, 3, 3));
        this.panelback2 = new tt2.QUI_Scale9(this.uiatals.GetSprite("panelback2"), new tt.Border(3, 3, 3, 3));
        this.panelback3 = new tt2.QUI_Scale9(this.uiatals.GetSprite("panelback3"), new tt.Border(4, 2, 4, 4));
        this.paneltitle3 = new tt2.QUI_Scale9(this.uiatals.GetSprite("paneltitle3"), new tt.Border(4, 3, 4, 3));
        this.btnroundsmall_no2 = this.uiatals.GetSprite("btnroundsmall_no2")
        this.btnroundsmall2 = this.uiatals.GetSprite("btnroundsmall2")
        this.icon1 = this.uiatals.GetSprite("icon1")
        this.icon2 = this.uiatals.GetSprite("icon2")
    }
    static CreateLabel(text?: string): tt2.QUI_Label {
        let label = new tt2.QUI_Label(this.font, text);
        label.localRect.setAsFill();
        return label;
    }
    static CreateWhiteImage(): tt2.QUI_Image {
        let img = new tt2.QUI_Image(this.white);
        img.localRect.setAsFill();
        return img;
    }
    static CreateDefaultBorder(): tt2.QUI_ImageScale9 {
        let title = new tt2.QUI_ImageScale9(this.border);
        title.localRect.setHPosFill();
        title.localRect.setVPosFill();
        return title;
    }
    static CreateDefaultBack(): tt2.QUI_ImageScale9 {
        let title = new tt2.QUI_ImageScale9(this.panelback2);
        title.localRect.setHPosFill();
        title.localRect.setVPosFill();
        return title;
    }
    static CreateDefaultPanel(): tt2.QUI_Panel {
        let winborder = new tt2.QUI_Panel();
        winborder.localRect.setVPosFill(16, 0);
        winborder.localRect.setHPosFill();
        winborder.getBorder().XLeft = 3;
        winborder.getBorder().XRight = 3;
        winborder.getBorder().YTop = 6;
        winborder.getBorder().YBottom = 3;

        //let border = new tt2.QUI_ImageScale9(this.border);
        //border.localRect.setAsFill();
        //winborder.borderElement = border;
        // let frame = new tt2.QUI_Container();

        // {
        //     // let title = new tt2.QUI_ImageScale9(this.paneltitle3);
        //     // title.localRect.setHPosFill();
        //     // title.localRect.setVPosByTopBorder(30);
        //     // frame.addChild(title);
        //     let border = new tt2.QUI_ImageScale9(this.border);
        //     frame.addChild(border);
        //     border.localRect.setHPosFill();
        //     border.localRect.setVPosFill();

        // }
        // //frame.localRect.setAsFill();
        winborder.backElement = new tt2.QUI_ImageScale9(this.panelback1);
        //winborder.borderElement = frame;

        return winborder;
    }
    static CreateHSplit(): tt2.QUI_Panel_Split {
        let split = new tt2.QUI_Panel_Split();
        split.localRect.setAsFill();
        let back1 = this.CreateWhiteImage();
        back1.color = new tt.Color(0.5, 0.5, 0.5, 0.5);
        split.getPanel1().addChild(back1);

        let back2 = this.CreateWhiteImage();
        back2.color = new tt.Color(0.5, 0.5, 0.5, 0.5);
        split.getPanel2().addChild(back2);

        split.getBorder().XLeft = 0;
        split.getBorder().XRight = 0;
        split.getBorder().YTop = 0;
        split.getBorder().YBottom = 0;
        let back = this.CreateWhiteImage();
        back.color = new tt.Color(0, 0, 0, 1);
        split.backElement = back;
        split.splitSize = 4;
        split.splitPos = 0.25;

        return split;
    }
    static CreateBackWithText(text: string, color: tt.Color) {
        let border = this.CreateDefaultBack();
        border.color = color.Clone();
        let label = new tt2.QUI_Label(this.font, text);
        border.addChild(label);
        label.color = color.Clone();
        //label.fontScale = new tt.Vector2(2.0, 2.0)
        label.localRect.setAsFill();
        return border;
    }
    static CreateCloseButton(): tt2.QUI_Button {
        let btn = new tt2.QUI_Button();
        btn.ElemNormal = this.CreateBackWithText("X", new tt.Color(1, 1, 1, 1));
        btn.ElemPress = this.CreateBackWithText("X", new tt.Color(1, 0, 0, 1));
        return btn;
    }
    static CreateButton(text: string): tt2.QUI_Button {
        let btn = new tt2.QUI_Button();
        btn.ElemNormal = this.CreateBackWithText(text, new tt.Color(1, 1, 1, 1));
        btn.ElemPress = this.CreateBackWithText(text, new tt.Color(1, 0, 0, 1));
        return btn;
    }
    static CreateToggle(text: string): tt2.QUI_Toggle {
        let btn = new tt2.QUI_Toggle();
        btn.ElemTrue = this.CreateBackWithText(text, new tt.Color(0, 0, 1, 1));
        btn.ElemTrueDown = this.CreateBackWithText(text, new tt.Color(0, 0, 0.5, 1));
        btn.ElemFalse = this.CreateBackWithText(text, new tt.Color(1, 1, 1, 1));
        btn.ElemFalseDown = this.CreateBackWithText(text, new tt.Color(0.5, 0.5, 0.5, 1));
        return btn;
    }

    static UpdateSubLabel(con: tt2.QUI_IElement, text: string, halign: tt2.QUI_HAlign = tt2.QUI_HAlign.Middle) {
        if (con == null)
            return;
        if (con.getElementType() == tt2.QUI_ElementType.Element_Label) {
            (con as tt2.QUI_Label).text = text;
            (con as tt2.QUI_Label).halign = halign;
        }

        if (con.getElementType() == tt2.QUI_ElementType.Element_Button) {
            let btn = con as tt2.QUI_Button;
            this.UpdateSubLabel(btn.ElemNormal, text);
            this.UpdateSubLabel(btn.ElemPress, text);
        }
        if (con.getElementType() == tt2.QUI_ElementType.Element_Toggle) {
            let btn = con as tt2.QUI_Toggle;
            this.UpdateSubLabel(btn.ElemFalse, text);
            this.UpdateSubLabel(btn.ElemFalseDown, text);
            this.UpdateSubLabel(btn.ElemTrue, text);
            this.UpdateSubLabel(btn.ElemTrueDown, text);
        }
        for (var i = 0; i < con.getChildCount(); i++) {
            let sube = con.getChild(i);
            this.UpdateSubLabel(sube, text);
        }
    }

}